﻿#ifndef WEBUI_HTTP_HELP_H__
#define WEBUI_HTTP_HELP_H__

#include <map>
#include <string>

#include <boost/spirit/include/classic.hpp>
#include <boost/spirit/include/classic_insert_at_actor.hpp>

namespace webui {
namespace http {

typedef std::map<std::string,std::string> map_ss_t;

std::string URLPath(const std::string& url)
{
	std::string r;
	using namespace BOOST_SPIRIT_CLASSIC_NS;
	parse( url.c_str(),	ch_p('/') >> (*( anychar_p - chset_p("? ") ))[assign_a(r)] );
	return r;
}

std::string Path2Ext(const std::string& path)
{
	std::string r;
	using namespace BOOST_SPIRIT_CLASSIC_NS;
	parse( path.c_str(), (*( anychar_p - '.' ))[assign_a(r)]%'.' );
	return r;
}

std::string URLQuery(const std::string& url)
{
	std::string r;
	using namespace BOOST_SPIRIT_CLASSIC_NS;
	parse( url.c_str(),	*(anychar_p-'?')>>'?'>>(*(anychar_p-' '))[assign_a(r)] );
	return r;
}


std::string Code2Str(int code)
{
	switch(code)
	{
	default: assert(!"invalid code");
	case 200: return "OK";
	case 400: return "Bad Request";
	case 403: return "Forbidden";
	case 404: return "Not Found";
	case 501: return "Not Implemented";
	case 503: return "Service Unavailable";
	}
}

std::string Ext2Type(const std::string& ext)
{
	if(ext=="js")
		return "text/javascript; charset=windows-1251";
	else
		return "text/html";
}

} // namespace http
} // namespace webui

#endif // WEBUI_HTTP_HELP_H__